<template>
  <div class="main-box">
    <el-button
      type="primary"
      style="margin-bottom: 16px"
      icon="el-icon-plus"
      @click="open"
    >
      新增会员等级
    </el-button>
    <BaseTable :request="request" ref="baseTable">
      <el-table-column prop="id" label="ID" width="120px" />
      <el-table-column prop="title" :label="$t('标题')" />
      <el-table-column label="图标">
        <template slot-scope="scope">
          <el-avatar
            v-if="scope.row.icon"
            shape="square"
            size="medium"
            :src="scope.row.icon.url"
          />
        </template>
      </el-table-column>
      <el-table-column prop="min_score" label="积分最低值" />
      <el-table-column prop="max_score" label="积分最低值" />
      <el-table-column prop="sort" :label="$t('排序')" />
      <el-table-column prop="create_time" label="添加时间" />
      <el-table-column prop="update_time" label="修改时间" />
      <el-table-column
        :label="$t('操作')"
        width="100px"
        fixed="right"
        align="right"
      >
        <template slot-scope="scope">
          <EditBtn @click="editUserGrade(scope.row)" />
          <DelBtn
            @success="delUserGrade(scope.row)"
            confirm-content="此操作将永久删除此会员等级, 是否继续?"
          />
        </template>
      </el-table-column>
    </BaseTable>
    <BaseDialog
      :title="adminForm.id ? '编辑会员等级' : '添加会员等级'"
      :dialog-show.sync="dialogAdmin"
      :width="'50'"
      footer
      @submit="submit"
    >
      <el-form
        ref="adminForm"
        :model="adminForm"
        :rules="adminRules"
        label-position="top"
      >
        <el-form-item label="标题：" prop="title">
          <el-input v-model="adminForm.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="等级图标：" prop="icon">
          <div class="attachment_imgBox" @click="openList('icon')">
            <img v-if="adminForm.icon" :src="iconUrl" class="attachmentImg" />
            <i v-else class="el-icon-camera" />
          </div>
          <Attachment
            ref="attachment"
            :dialog-show.sync="dialogShow"
            :attachment-type="attachmentType"
            @func="getData"
          />
        </el-form-item>
        <el-form-item label="积分最小值：" prop="min_score">
          <el-input
            v-model="adminForm.min_score"
            placeholder="请输入积分最小值"
          />
        </el-form-item>
        <el-form-item label="积分最大值：" prop="max_score">
          <el-input
            v-model="adminForm.max_score"
            placeholder="请输入积分最大值"
          />
        </el-form-item>
        <el-form-item label="排序权重：" prop="sort">
          <el-input v-model="adminForm.sort" />
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>
<script>
import userApi from '@/api/users.js'
export default {
  data() {
    return {
      request: userApi.userGradeList,
      dialogShow: false,
      attachmentType: '',
      iconUrl: '',
      dialogAdmin: false,
      name: '', // 暂存会员等级姓名
      adminForm: {
        title: '',
        icon: '',
        min_score: '',
        max_score: '',
        sort: 0,
        id: ''
      }, // 新增会员等级表单
      adminRules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        min_score: [
          { required: true, message: '请输入积分最小值', trigger: 'blur' }
        ],
        max_score: [
          { required: true, message: '请输入积分最大值', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    getData(data) {
      if (data.length > 1) {
        this.$message.error(this.$t('最多只能选择一张图片'))
      } else {
        if (this.uploadType == 'icon') {
          this.adminForm.icon = data[0].id
          this.iconUrl = data[0].url
        }
        this.dialogShow = false
      }
    },
    // 打开附件弹窗
    openList(val) {
      this.dialogShow = true
      this.attachmentType = 2
      this.$nextTick(() => {
        this.$refs.attachment.getAttachmentList()
        this.$refs.attachment.$refs.AttachmentCate.getCateList(0)
      })
      this.uploadType = val
    },
    open() {
      this.dialogAdmin = true
      this.adminForm = {
        title: '',
        desc: '',
        id: ''
      }
    },
    editUserGrade(row) {
      this.dialogAdmin = true
      this.adminForm = row
      if (row.icon) {
        this.adminForm.icon = row.icon.id
        this.iconUrl = row.icon.url
      }
    },
    delUserGrade(row) {
      userApi.userGradeDel({ id: row.id }).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.getList()
        }
      })
    },
    // 获取列表
    getList(type) {
      this.$refs.baseTable.getList(type)
    },

    // 提交
    submit() {
      this.$refs.adminForm.validate(valid => {
        if (valid) {
          if (!this.adminForm.id) {
            userApi.userGradeAdd({ ...this.adminForm }).then(res => {
              if (res.code === 0) {
                this.$message.success(res.msg)
                this.dialogAdmin = false
                this.getList('search')
              }
            })
          } else {
            userApi.userGradeEdit({ ...this.adminForm }).then(res => {
              if (res.code === 0) {
                this.$message.success(res.msg)
                this.dialogAdmin = false
                this.getList()
              }
            })
          }
        } else {
          this.$message.warning(this.$t('请填写必要信息'))
        }
      })
    }
  }
}
</script>
<style lang="scss">
#admin {
  .el-form-item {
    width: 100%;
  }
  .el-form-item__content {
    width: calc(100% - 130px);
    .el-select {
      width: calc(100%);
    }
  }
}
</style>
<style lang="scss" scoped>
.el-avatar {
  background: #eff0f1;
}
.tabs {
  padding: 15px 10px;
}

#admin {
  background-color: #fff;
}

.el-table {
  margin-bottom: 20px;
}
.search {
  background-color: #fff;
  padding: 15px 10px;
  margin-bottom: 10px;
  .el-form-item {
    margin-bottom: 0;
  }
  .el-button {
    font-size: 14px;
  }
}
.btn {
  text-align: center;
}
</style>
